home *** CD-ROM | disk | FTP | other *** search
/ com!online 2002 July / com!online0702.iso / software / livemotion / DATA1.CAB / Scripting_Resources / Samples / Automation_Scripts / Create Shadow.js < prev    next >
Encoding:
Text File  |  2002-05-13  |  7.5 KB  |  215 lines

  1. //////////////////////////////////////////////////
  2. //
  3. // ADOBE SYSTEMS INCORPORATED 
  4. // Copyright 2002 Adobe Systems Incorporated 
  5. // All Rights Reserved 
  6. //
  7. // NOTICE:  Adobe permits you to use, modify, and 
  8. // distribute this file in accordance with the terms
  9. // of the Adobe license agreement accompanying it.  
  10. // If you have received this file from a source 
  11. // other than Adobe, then your use, modification,
  12. // or distribution of it requires the prior 
  13. // written permission of Adobe. 
  14. //
  15. //////////////////////////////////////////////////
  16.  
  17. //////////////////////////////////////////////////
  18. // Create Shadow.js
  19. // 
  20. // DESCRIPTION
  21. //
  22. // This script demonstrates the use of Effect types and creating and naming
  23. // groups. This script creates a shadow for any selected object and gives it
  24. // effects and depths. It groups them so that the shadow moves alongwith the
  25. // object and also names them as one group. Then it animates them holding at
  26. // each keyframe.
  27. //
  28. // HOW TO USE
  29. //
  30. // Create any object(s) and select it in the Composition.
  31. // Select Automation > Run Automation Scripts > Create Shadow.js
  32. // 
  33. // You can then animate them as one object.
  34. // Note: Make sure your object is any color other than black which differs
  35. // from black considerably.
  36. // 
  37. // The other alternative to do the same would be to create a layer underneath
  38. // the object. They would then be one object and grouping will not be required.
  39. //
  40. //////////////////////////////////////////////////
  41.  
  42.  
  43. // Main Code [Execution of script begins from here]
  44.  
  45. // Check if any composition is open
  46. if(application.compositions.length > 0){
  47.     comp = application.currentComposition;
  48.     if(comp.selection.length >= 1){ // Checks if at least one object is selected
  49.         var source = comp.selection[0]; // Store all selected objects in the array source
  50.         application.currentComposition.saveSelection(); // saves the current selection
  51.     
  52.         createShadow(comp, source);// Function createShadow called
  53.         // The current composition  and the selected objects are passed as arguements.
  54.     
  55.         application.currentComposition.restoreSelection(); // restores the saved selection
  56.     }
  57.     else{ // If no objects are selected brings the Console window up 
  58.         Console.show();
  59.         // Writes to the Console
  60.         Console.write("Please select the objects to create shadow and run script again.\n");
  61.     }
  62. }
  63. else{// if no composition open
  64.     // opens a new composition
  65.     comp = application.newComposition();
  66.     Console.show();
  67.     Console.write("New Composition opened\nPlease create and select the objects to create shadow of and run script again.\n");
  68. }
  69.  
  70.  
  71. // Add your own functions here
  72.  
  73. //////////////////////////////////////////////////
  74. //
  75. // createShadow:
  76. // 
  77. // Creates the shadow of the object passed to it groups them 
  78. // and then animates treating them as one object.
  79. //
  80. // source: The currently selected objects which are passed to the function.
  81. // comp: The current composition.
  82. //
  83. //////////////////////////////////////////////////
  84.  
  85. function createShadow(comp, source)
  86. {
  87.     var shadow;// variable to store shadow of object
  88.     var xdiff; // Variable to store the difference in x sizes of source and shadow
  89.     
  90.     // Determining the distance between the shadow and the source object
  91.     if (source.size.x >= 0 && source.size.x <= 100){ 
  92.     // if object size is between 0-100
  93.         xdiff = 2;
  94.     } 
  95.     else {// if object size is greater than 100
  96.         xdiff = 3;
  97.     }
  98.         
  99.     // Creating a duplicate object which is the shadow.
  100.     shadow = source.duplicate();
  101.         
  102.     // Setting its start color of gradient without setting 
  103.     // the gradient type. This is how we can set a single color 
  104.     // for objects without gradient. We are setting black color 
  105.     // for the shadow here.
  106.     shadow.layers[0].colorGradient.startColor.red = 0;
  107.     shadow.layers[0].colorGradient.startColor.green = 0;
  108.     shadow.layers[0].colorGradient.startColor.blue = 0;
  109.         
  110.     // Move the shadow size to give it a slight 3D Effect.
  111.     shadow.size.x += xdiff;
  112.         
  113.     // Note: The shadow is created on top of the original object. 
  114.     // The order in which the objects get/are created determines 
  115.     // the z-order of the objects. But here since shadows are below 
  116.     // the original objects we use LMArrangeType to bring the object
  117.     // in front. 
  118.         
  119.     source.arrange(LMArrangeType.bringToFront);
  120.     // We can also use bringToFront instead of bringForwards 
  121.     // here since we have only two objects
  122.     
  123.     // Give the Effect type emboss to the source.
  124.     source.layers[0].effect.type = LMEffectType.emboss;
  125.         
  126.     // Set the depth of the layer for effect as 5.
  127.     source.layers[0].effect.depth = 5;
  128.     Console.write(source + "\n");
  129.         
  130.     // Note: Original object source and shadow move together so
  131.     // we group them and then we can manipulate them as one object.
  132.     
  133.     // To group the shadow and the original source object 
  134.     // create an array of objects to group.
  135.     var objstoGroup = new Array(source, shadow);
  136.     
  137.     // Using the group method of composition to group the array.
  138.     var newGroup = comp.group(objstoGroup);
  139.     Console.write(newGroup);
  140.     
  141.     // Naming the group using the name property. 
  142.     // Every object has a name property.
  143.     newGroup.name = "myGroup";
  144.     // if you have more than one object to name then you can 
  145.     // give it in a loop as "myGroup"+i.
  146.     // This will name the objects as myGroup0, myGroup1 and so on.
  147.     
  148.     // Assigning the opacity to oriOpacity        
  149.     var oriOpacity = newGroup.opacity;
  150.     
  151.     // Starting the opacity stopwatch
  152.     newGroup.stopwatch.opacity = true;
  153.     
  154.     // Assign the original opacity 
  155.     newGroup.opacity = oriOpacity;
  156.     
  157.     Scaling(newGroup, 3, 36, 10); // Function Scaling called
  158.     
  159.     // Note: In Automation scripts you refer to object through 
  160.     // the names you give them in the script. Unlike in player 
  161.     // scripts you refer to them through their names as they appear 
  162.     // on the timeline.
  163.     
  164.     // Opacity is 0
  165.     newGroup.opacity = 0;
  166. }
  167.  
  168. //////////////////////////////////////////////////
  169. // Scaling:
  170. // 
  171. // Animates the object and its shadow as one object holding 
  172. // keyframe at every scale change.
  173. //
  174. // myObject: The grouped object and shadow, now one object.
  175. // keyFrameRate: The rate at which the keyframes will be set.
  176. // frames: Total number of frames across which the object is animated
  177. // increment: The amount of scale increased every keyframe.
  178. //////////////////////////////////////////////////
  179.  
  180. function Scaling(myObject, keyFrameRate, frames, increment)
  181. {
  182.     var xscale = myObject.scale.x; // Variable for x scale.
  183.     var yscale = myObject.scale.y; // Variable for y scale.
  184.     var f, frame0; // Variables for setting current frame and first frame respectively.
  185.     
  186.     // Start the stopwatch for scaling.
  187.     myObject.stopwatch.scale = true;
  188.     
  189.     // Since starting does not record the attribute assign 
  190.     // it again to record it's value
  191.     myObject.scale.x = xscale ;
  192.     myObject.scale.y = yscale ;
  193.         
  194.     // First frame is the current frame. 
  195.     frame0 = myObject.startFrame; 
  196.     
  197.     // Loop for gradual increase in size of the object 
  198.     for( f=0 ; f <= frames ; f+=keyFrameRate){
  199.         // Set the key frame at the current frame
  200.         myObject.currentFrame = frame0 + f;
  201.         
  202.         // Increase opacity by increment value
  203.         myObject.scale.x += increment;
  204.         myObject.scale.y += increment;
  205.     }
  206.     
  207.     // The keyframes set for the object can be accessed by using -
  208.     // object.keyframes.attribute. Here myObject.keyframes.scale.
  209.     var kfs = myObject.keyframes.scale;
  210.     
  211.     // To turn the boolean hold on for each keyframe looping through all
  212.     // keyframes in the array kfs.
  213.     for (var index = 0;index < kfs.length;++index)
  214.         kfs[index].hold = true;
  215. }